local yishe = fk.CreateSkill {

  name = "joy__yishe",

  tags = {  },

}



yishe:addEffect(fk.EventPhaseStart, {
  name = "joy__yishe",
  anim_type = "support",
  derived_piles = "joy__zhanglu_mi",
  events = {fk.EventPhaseStart, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(yishe.name) then
      if event == fk.EventPhaseStart then
        return target == player and player.phase == Player.Finish 
      else
        if #player:getPile("joy__zhanglu_mi") == 0 and player:isWounded() then
          for _, move in ipairs(data) do
            if move.from==player then
              for _, info in ipairs(move.moveInfo) do
                if info.fromSpecialName == "joy__zhanglu_mi" then
                  return true
                end
              end
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return player.room:askForSkillInvoke(player, yishe.name, nil, "#joy__yishe-invoke")
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      player:drawCards(2, yishe.name)
      if player:isNude() then return end
      local dummy = Fk:cloneCard("dilu")
      local cards
      if #player:getCardIds("he") < 3 then
        cards = player:getCardIds("he")
      else
        cards = room:askForCard(player, 2, 2, true, yishe.name, false, ".", "#joy__yishe-cost")
      end
      dummy:addSubcards(cards)
      player:addToPile("joy__zhanglu_mi", dummy, true, yishe.name)
    else
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = yishe.name
      })
    end
  end,
})
yishe:addEffect(fk.AfterCardsMove, {
  name = "joy__yishe",
  anim_type = "support",
  derived_piles = "joy__zhanglu_mi",
  events = {fk.EventPhaseStart, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(yishe.name) then
      if event == fk.EventPhaseStart then
        return target == player and player.phase == Player.Finish 
      else
        if #player:getPile("joy__zhanglu_mi") == 0 and player:isWounded() then
          for _, move in ipairs(data) do
            if move.from==player then
              for _, info in ipairs(move.moveInfo) do
                if info.fromSpecialName == "joy__zhanglu_mi" then
                  return true
                end
              end
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return player.room:askForSkillInvoke(player, yishe.name, nil, "#joy__yishe-invoke")
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      player:drawCards(2, yishe.name)
      if player:isNude() then return end
      local dummy = Fk:cloneCard("dilu")
      local cards
      if #player:getCardIds("he") < 3 then
        cards = player:getCardIds("he")
      else
        cards = room:askForCard(player, 2, 2, true, yishe.name, false, ".", "#joy__yishe-cost")
      end
      dummy:addSubcards(cards)
      player:addToPile("joy__zhanglu_mi", dummy, true, yishe.name)
    else
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = yishe.name
      })
    end
  end,
})

return yishe